package org.dromara.bean.service.impl;

import cn.hutool.core.util.ObjectUtil;
import org.dromara.bean.constant.OrderStatusConstant;
import org.dromara.bean.domain.HisGoodsDetails;
import org.dromara.bean.domain.HisGoodsOrder;
import org.dromara.bean.domain.HisRegistration;
import org.dromara.bean.mapper.HisGoodsDetailsMapper;
import org.dromara.bean.mapper.HisGoodsOrderMapper;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.ReturnOrderModel;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.bean.domain.bo.HisGoodsBo;
import org.dromara.bean.domain.vo.HisGoodsVo;
import org.dromara.bean.domain.HisGoods;
import org.dromara.bean.mapper.HisGoodsMapper;
import org.dromara.bean.service.IHisGoodsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品spuService业务层处理
 *
 * @author Lion Li
 * @date 2024-12-13
 */
@RequiredArgsConstructor
@Service
public class HisGoodsServiceImpl implements IHisGoodsService {

    private final HisGoodsMapper baseMapper;

    private final HisGoodsOrderMapper orderMapper;

    private final HisGoodsDetailsMapper detailsMapper;

    /**
     * 查询商品spu
     *
     * @param id 主键
     * @return 商品spu
     */
    @Override
    public HisGoodsVo queryById(Long id) {
        HisGoodsVo hisGoodsVo = baseMapper.selectVoById(id);
        // TODO 常用的一些代码可以写注解直接在实体类里面引入注解搞定，但是没时间，想起来了再写
        if (ObjectUtil.isNotEmpty(hisGoodsVo.getLabels())) {
            hisGoodsVo.setLabel(StringUtils.split(hisGoodsVo.getLabels(), ","));
        }
        return hisGoodsVo;
    }

    /**
     * 分页查询商品spu列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 商品spu分页列表
     */
    @Override
    public TableDataInfo<HisGoodsVo> queryPageList(HisGoodsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HisGoods> lqw = buildQueryWrapper(bo);
        Page<HisGoodsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.getRecords().forEach(r -> {
            if (ObjectUtil.isNotEmpty(r.getLabels())) {
                r.setLabel(StringUtils.split(r.getLabels(), ","));
            }
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的商品spu列表
     *
     * @param bo 查询条件
     * @return 商品spu列表
     */
    @Override
    public List<HisGoodsVo> queryList(HisGoodsBo bo) {
        LambdaQueryWrapper<HisGoods> lqw = buildQueryWrapper(bo);
        List<HisGoodsVo> hisGoodsVos = baseMapper.selectVoList(lqw);
        hisGoodsVos.forEach(r -> {
            if (ObjectUtil.isNotEmpty(r.getLabels())) {
                r.setLabel(StringUtils.split(r.getLabels(), ","));
            }
        });
        return hisGoodsVos;
    }

    private LambdaQueryWrapper<HisGoods> buildQueryWrapper(HisGoodsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HisGoods> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), HisGoods::getTitle, bo.getTitle());
        lqw.eq(bo.getListing() != null, HisGoods::getListing, bo.getListing());
        lqw.eq(HisGoods::getDelFlag, 0);
        lqw.orderByDesc(HisGoods::getCreateTime);
        return lqw;
    }

    /**
     * 新增商品spu
     *
     * @param bo 商品spu
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HisGoodsBo bo) {
        HisGoods add = MapstructUtils.convert(bo, HisGoods.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品spu
     *
     * @param bo 商品spu
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HisGoodsBo bo) {
        HisGoods update = MapstructUtils.convert(bo, HisGoods.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HisGoods entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除商品spu信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }


//-------------------------------------------小程序----------------------------------------------------------------------------

    /**
     * 分页查询商品spu列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 商品spu分页列表
     */
    @Override
    public TableDataInfo<HisGoodsVo> xcxQueryPageList(HisGoodsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HisGoods> lqw = xcxBuildQueryWrapper(bo);
        Page<HisGoodsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.getRecords().forEach(r -> {
            if (ObjectUtil.isNotEmpty(r.getLabels())) {
                r.setLabel(StringUtils.split(r.getLabels(), ","));
            }
        });
        return TableDataInfo.build(result);
    }

    private LambdaQueryWrapper<HisGoods> xcxBuildQueryWrapper(HisGoodsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HisGoods> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTitle()), HisGoods::getTitle, bo.getTitle());
        lqw.eq(HisGoods::getListing, 0);
        lqw.eq(HisGoods::getDelFlag, 0);
        if (bo.getOrderByColumn().equals("desc")){
            lqw.orderByDesc(HisGoods::getPrice);
        }else if (bo.getOrderByColumn().equals("asc")){
            lqw.orderByAsc(HisGoods::getPrice);
        }
        return lqw;
    }
}
